﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace TestApp.Flowchart.Model
{
    [JsonObject(MemberSerialization.OptIn)]
    class Conv1 : FlowNode
    {
        private int output_kernal_number = 1;

        private int _inputChannel = 1;
        [JsonProperty]
        public int InputChannel
        {
            get { return _inputChannel; }
            set
            {
                _inputChannel = value;
                OnPropertyChanged("InputChannel");
            }
        }

        private int _outputChannel = 1;
        [JsonProperty]
        public int OutputChannel
        {
            get { return _outputChannel; }
            set
            {
                _outputChannel = value;
                OnPropertyChanged("OutputChannel");
            }
        }

        private int _kernelPadding = 5;
        [JsonProperty]
        public int KernelPadding
        {
            get { return _kernelPadding; }
            set
            {
                _kernelPadding = value;
                OnPropertyChanged("KernelPadding");
            }
        }

        private int _kernelNumber = 5;
        [JsonProperty]
        public int KernelNumber
        {
            get { return _kernelNumber; }
            set
            {
                _kernelNumber = value;
                OnPropertyChanged("KernelNumber");
            }
        }

        private int _kernelThickness = 1;
        [JsonProperty]
        public int KernelThickness
        {
            get { return _kernelThickness; }
            set
            {
                _kernelThickness = value;
                OnPropertyChanged("KernelThickness");
            }
        }

        private string _kernelBorderColor = "255, 0, 0, 0";
        [JsonProperty]
        public string KernelBorderColor
        {
            get { return _kernelBorderColor; }
            set
            {
                _kernelBorderColor = value;
                OnPropertyChanged("KernelBorderColor");
            }
        }

        private string _lineColor = "255, 0, 255, 0";
        [JsonProperty]
        public string LineColor
        {
            get { return _lineColor; }
            set
            {
                _lineColor = value;
                OnPropertyChanged("LineColor");
            }
        }

        private int _lineThickness = 1;
        [JsonProperty]
        public int LineThickness
        {
            get { return _lineThickness; }
            set
            {
                _lineThickness = value;
                OnPropertyChanged("LineThickness");
            }
        }

        private int _stride = 1;
        [JsonProperty]
        public int Stride
        {
            get { return _stride; }
            set
            {
                _stride = value;
                OnPropertyChanged("Stride");
            }
        }

        private string _fromCorner = "10,10";
        [JsonProperty]
        public string FromCorner
        {
            get { return _fromCorner; }
            set
            {
                _fromCorner = value;
                OnPropertyChanged("FromCorner");
            }
        }

        private string _toCorner = "90,20";
        [JsonProperty]
        public string ToCorner
        {
            get { return _toCorner; }
            set
            {
                _toCorner = value;
                OnPropertyChanged("ToCorner");
            }
        }
        
        public Conv1() { }

        public override FrameworkElement CreateContent()
        {
            var textBlock = base.CreateContent() as TextBlock;

            var canvas = new Canvas();

            Line myLine = new Line();
            myLine = new Line();

            var list1 = UIUtils.convertString(_fromCorner);


            Rectangle rect = new Rectangle();
            
            var mySolidColorBrushBack = new SolidColorBrush();
            mySolidColorBrushBack.Color = UIUtils.convertRGB("128, 255,0,0");
            rect.Fill = mySolidColorBrushBack;

            rect.Width = _kernelNumber * _kernelPadding;
            rect.Height = _kernelNumber * _kernelPadding;


            Canvas.SetLeft(rect, list1[0]);
            Canvas.SetTop(rect, list1[1]);
            canvas.Children.Add(rect);




            var lineList1 = UIUtils.draw_table(list1[0], list1[1], _kernelNumber, _kernelPadding);
            foreach (var line in lineList1)
            {
                var mySolidColorBrush = new SolidColorBrush();
                mySolidColorBrush.Color = UIUtils.convertRGB(this._kernelBorderColor);
                line.Stroke = mySolidColorBrush;
                line.StrokeThickness = _kernelThickness;
                canvas.Children.Add(line);
            }


            var list2 = UIUtils.convertString(_toCorner);
            var lineList2 = UIUtils.draw_table(list2[0], list2[1], output_kernal_number, _kernelPadding);
            foreach (var line in lineList2)
            {
                var mySolidColorBrush = new SolidColorBrush();
                mySolidColorBrush.Color = UIUtils.convertRGB(this._kernelBorderColor);
                line.Stroke = mySolidColorBrush;
                line.StrokeThickness = _kernelThickness;
                canvas.Children.Add(line);
            }


            var mySolidColorBrush1 = new SolidColorBrush();
            mySolidColorBrush1.Color = UIUtils.convertRGB(this._lineColor);
            myLine.Stroke = mySolidColorBrush1;

            myLine.StrokeThickness = _lineThickness;
            myLine.X1 = list1[0] + _kernelNumber * _kernelPadding;
            myLine.Y1 = list1[1];
            myLine.X2 = list2[0];
            myLine.Y2 = list2[1];

            canvas.Children.Add(myLine);

            myLine = new Line();

            var mySolidColorBrush2 = new SolidColorBrush();
            mySolidColorBrush2.Color = UIUtils.convertRGB(this._lineColor);
            myLine.Stroke = mySolidColorBrush2;

            myLine.StrokeThickness = _lineThickness;
            myLine.X1 = list1[0] + _kernelNumber * _kernelPadding;
            myLine.Y1 = list1[1] + _kernelNumber * _kernelPadding;
            myLine.X2 = list2[0];
            myLine.Y2 = list2[1] + output_kernal_number * _kernelPadding;

            canvas.Children.Add(myLine);


            Canvas.SetLeft(textBlock, (list1[0] + myLine.X2 + output_kernal_number * _kernelPadding) / 2);            
            Canvas.SetTop(textBlock, Math.Max(myLine.Y1, myLine.Y2) + 10);

            canvas.Children.Add(textBlock);

            return canvas;
        }

        public virtual FlowNode Json2Obj(string value)
        {
            return JSONUtils.Json2Obj<Conv1>(value);
        }
    }
}